package notes

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"tao-admin/utils"
)

type NoteView struct {
	NoteService *NoteService
}

func NewNoteView() *NoteView {
	return &NoteView{
		NoteService: NewNoteService(),
	}
}

func (view *NoteView) searchNoteView(ctx *gin.Context) {
	res := utils.NewSimpleRes()
	planInfos, total, err := view.NoteService.GetNoteInfos(ctx)
	if err != nil {
		resp := fmt.Sprintf("error:%v", err)
		res.Retcode = -1
		res.Message = "failed"
		res.Data = map[string]interface{}{
			"err": err,
			"msg": resp,
		}
	} else {
		res.Data = map[string]interface{}{
			"items": planInfos,
			"total": total,
		}
	}
	ctx.JSON(200, res)
}

func (view *NoteView) createNoteView(ctx *gin.Context) {
	res := utils.NewSimpleRes()
	err, msg := view.NoteService.CreateNoteItem(ctx)
	if err != nil || msg != "" {
		res.Retcode = -1
		res.Message = "failed"
		res.Data = map[string]interface{}{
			"err": err,
			"msg": msg,
		}
	}
	ctx.JSON(200, res)
}
func (view *NoteView) updateNoteView(ctx *gin.Context) {
	res := utils.NewSimpleRes()
	err, msg := view.NoteService.UpdateNoteItem(ctx)
	if err != nil || msg != "" {
		res.Retcode = -1
		res.Message = "failed"
		res.Data = map[string]interface{}{
			"err": err,
			"msg": msg,
		}
	}
	ctx.JSON(200, res)
}

func (view *NoteView) deleteNoteView(ctx *gin.Context) {
	res := utils.NewSimpleRes()
	err, msg := view.NoteService.DeletePlanItem(ctx)
	if err != nil || msg != "" {
		res.Retcode = -1
		res.Message = "failed"
		res.Data = map[string]interface{}{
			"err": err,
			"msg": msg,
		}
	}
	ctx.JSON(200, res)
}

func (view *NoteView) getNoteInfoView(ctx *gin.Context) {
	fmt.Println("Plan定时任务：查找未完成的计划项！！！")
}
